package com.yanqu.road.server.cmd.activity.feast.pvp;

import com.yanqu.road.entity.activity.feast.config.ShanHeFeastConfig;
import com.yanqu.road.entity.activity.feast.data.FeastUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ShanHeFeastProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.feast.ShanHeFeastModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.feast.ShanHeFeastMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@CmdApi(reqMsg = ShanHeFeastProto.FeastPvpUserApplyReqMsg.class, respMsg = ShanHeFeastProto.FeastPvpUserApplyRespMsg.class)
@Cmd(code = GameProtocol.S_SHAN_HE_FEAST_PVP_USER_APPLY, desc = "pvp报名")
public class FeastPvpUserApplyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShanHeFeastProto.FeastPvpUserApplyReqMsg reqMsg = ShanHeFeastProto.FeastPvpUserApplyReqMsg.parseFrom(packet.getBytes());
        //获取活动配置
        ShanHeFeastConfig feastConfig = ShanHeFeastMgr.getConfig();
        if (feastConfig == null || !ActivityMgr.activityInShowTime(feastConfig.getActivityInfo())) {
            ret = GameErrorCode.E_SHAN_HE_FEAST_ACTIVITY_NOT_FOUND;
            return;
        }
        FeastUserData userData = player.getModule(ShanHeFeastModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        //0才力可以报名
        if(userData.getPowerVal() <= 0 && reqMsg.getType() == 1){
            ret = GameErrorCode.E_SHAN_HE_FEAST_PVP_POWER_VAL_NO_ENOUGH;
            return;
        }
        // 加点东西
        ShanHeFeastProto.FeastPvpUserApplyReqMsg.Builder builder = reqMsg.toBuilder();
        builder.setStateId(userData.getStateId());
        builder.setPowerVal(userData.getPowerVal());
        builder.setPlayerInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        // 上跨服ActivityLotteryActivity
        player.sendPacket(Protocol.C_CROSS_SHAN_HE_FEAST_PVP_USER_APPLY, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ShanHeFeastProto.FeastPvpUserApplyRespMsg.Builder respMsg = ShanHeFeastProto.FeastPvpUserApplyRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_SHAN_HE_FEAST_PVP_USER_APPLY, respMsg);
        }
    }
}
